package com.lenovo.smarttraffic.ui.activity.stage2;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarInfo;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.ToastUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.util.Timer;
import java.util.TimerTask;

public class ETCChargeActivity extends BaseActivity {

    private EditText car_id_et;

    private TextView car_number;

    private EditText car_money_et;

    private Button charge_btn;

    private CarInfo carInfo;

    @Override
    protected int getLayout() {
        return R.layout.activity_etc_charge;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        HttpUtils.post(ETCChargeActivity.this, "get_car_info",
                new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> carInfo = new Gson().fromJson(response.toString(), CarInfo.class));

        initView();
        initData();
    }

    public void initView() {
        Toolbar mToolbar = findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(view -> finish());


        car_id_et = findViewById(R.id.car_id_et);
        car_id_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0 && carInfo != null && Utils.parseInt(s.toString()) < 500) {
                    car_number.setText(carInfo.getROWS_DETAIL().get(Utils.parseInt(s.toString())).getCarnumber());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        car_number = findViewById(R.id.car_number);

        car_money_et = findViewById(R.id.car_money_et);


        findViewById(R.id.money_10).setOnClickListener(v -> car_money_et.setText("10"));
        findViewById(R.id.money_50).setOnClickListener(v -> car_money_et.setText("50"));
        findViewById(R.id.money_100).setOnClickListener(v -> car_money_et.setText("100"));


        charge_btn = findViewById(R.id.charge_btn);
        charge_btn.setOnClickListener(v -> {
            int money = Utils.parseInt(car_money_et.getText().toString());

            if (car_id_et.getText().toString().isEmpty()) {
                ToastUtils.showToast(this, "请输入车辆编号", Toast.LENGTH_SHORT);
            } else if (car_id_et.getText().toString().isEmpty()) {
                ToastUtils.showToast(this, "请输入充值金额", Toast.LENGTH_SHORT);
            } else if (money < 1 || money > 999) {
                ToastUtils.showToast(this, "充值金额在1-999元之间", Toast.LENGTH_SHORT);
            } else {
                ProgressDialog progressDialog = new ProgressDialog(v.getContext());
                progressDialog.setTitle("请求状态");
                progressDialog.setMessage("网络请求中");
                progressDialog.setCancelable(false);
                progressDialog.show();

                HttpUtils.post(v.getContext(), "get_car_info", new HttpUtils().setParam("UserName", MainActivity.userName), response -> {
                    if (Utils.getResult(response)) {
                        ToastUtils.showToast(ETCChargeActivity.this, "网络请求成功", Toast.LENGTH_SHORT);
                        HttpUtils.post(this, "set_car_account_recharge",
                                new HttpUtils().setParam("CarId", Utils.parseInt(car_id_et.getText().toString())).
                                        setParam("Money", money).setParam("UserName", MainActivity.userName),
                                response2 -> {
                                    if (Utils.getResult(response2)) {
                                        ToastUtils.showToast(this, "充值成功", Toast.LENGTH_SHORT);
                                    } else {
                                        ToastUtils.showToast(this, "充值失败", Toast.LENGTH_SHORT);
                                    }
                                });
                    } else {
                        ToastUtils.showToast(ETCChargeActivity.this, "网络请求失败", Toast.LENGTH_SHORT);
                    }
                    progressDialog.dismiss();
                });

                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if (progressDialog.isShowing()) {
                            progressDialog.dismiss();
                            ToastUtils.showToast(ETCChargeActivity.this, "网络连接失败", Toast.LENGTH_SHORT);
                        }
                    }
                }, 4000);

            }
        });

    }

    public void initData() {

    }
}
